/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cms.sessionbean;

import cms.dataaccess.DAOFactory;
import dto.Conference;
import dto.Participant;
import dto.ParticipantId;
import dto.Timeline;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;

/**
 *
 * @author TauPo
 */
@Stateless(mappedName="ConferenceApplications")
public class ConferenceApplications implements ConferenceApplicationsRemote {
   
    @Override
    public boolean saveConference(Conference conf) {
        try {
         DAOFactory datafactory = DAOFactory.getInstance();
         datafactory.getConferenceDAO().saveUpdateConfernce(conf);
        return true;
        }
        catch(Exception e){
           System.err.println(e.getMessage());
           return false;
        }
    }

    @Override
    public boolean updateConference(Conference conf) {
        try {
         DAOFactory datafactory = DAOFactory.getInstance();
         datafactory.getConferenceDAO().saveUpdateConfernce(conf);
        return true;
        }
        catch(Exception e){
           System.err.println(e.getMessage());
           return false;
        }
    }

    @Override
    public List<Conference> getAllConferences() {
       try {
        DAOFactory datafactory = DAOFactory.getInstance();
        return  datafactory.getConferenceDAO().getAllConference();
      
        }
         catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public List<Conference> getAllOpenConferences() {
       try {
        DAOFactory datafactory = DAOFactory.getInstance();
        List<Timeline> aList = datafactory.getTimeLineDAO().findByCustomizeQuery("holdFrom>=sysdate()");
        ArrayList<Conference> openConList = new  ArrayList<Conference> ();
        for(int i =0;i<aList.size();i++){
            String conferenceName = aList.get(i).getConferenceName();
            Conference openConf = this.getConference(conferenceName);
            if(openConf !=null){
                openConList.add(openConf);
            }
            
        }
        
        return openConList;
      
        }
       catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public List<Conference> getConferencesByBookedUser(String userName) {
      try {
        DAOFactory datafactory = DAOFactory.getInstance();
        ArrayList<Conference> ConList=new  ArrayList<Conference>();
        List<Participant> parttList=  datafactory.getParticipantDAO().findParticipantByUserName(userName);
        for(Participant p:parttList){
            String confName =  p.getId().getConferenceName();
            Conference bkcon = getConference(confName);
            if(confName!=null){
                ConList.add(bkcon);
            }
        }
        return ConList;
      }
      catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public Participant participantConference(String userName, String confName) {
       try {
           
         Conference conf =this.getConference(confName);
         if(conf == null){
            System.err.println("confName:"+confName+ "is invalidate");
            return null;
         }
        if(DAOFactory.getInstance().getUserDAO().findUser(userName)==null){
            System.err.println("user Name:"+userName+ "is invalidate");
            return null;
        }
            
        ParticipantId  regID = new ParticipantId(conf.getConferenceName(),userName);
             DateFormat formatter =new SimpleDateFormat("yyyy/mm/dd hh:mm:ss");
            java.util.Date dt = new java.util.Date();
            String currentTime = formatter.format(dt);
            Date regDate = formatter.parse(currentTime);
           
        Participant regist  =  new Participant(regID,0,regDate,conf.getPrice());
        DAOFactory datafactory = DAOFactory.getInstance();
        datafactory.getParticipantDAO().insertParticipant(regist);
        return regist;
      }
      catch(Exception e){
           System.err.println(e.getMessage());
           return null;
      }
    
    }
    @Override
    public boolean unParticipantConference(String userName, String confName) {
      try {
           
         Conference conf =this.getConference(confName);
         if(conf == null){
            System.err.println("confName:"+confName+ "is invalidate");
            return false;
         }
        if(DAOFactory.getInstance().getUserDAO().findUser(userName)==null){
            System.err.println("user Name:"+userName+ "is invalidate");
            return false;
        }
            
        ParticipantId  regID = new ParticipantId(conf.getConferenceName(),userName);
        Participant regist  =  new Participant(regID);
        DAOFactory datafactory = DAOFactory.getInstance();
        datafactory.getParticipantDAO().deleteParticipant(regist);
        return true;
      }
      catch(Exception e){
           System.err.println(e.getMessage());
           return false;
      }
    
    }

    @Override
    public boolean updateTimeLine(Timeline timeline) {
         try {
         DAOFactory datafactory = DAOFactory.getInstance();
         datafactory.getTimeLineDAO().updateTimeline(timeline);
        return true;
        }
        catch(Exception e){
           System.err.println(e.getMessage());
           return false;
        }
    }

   
    @Override
    public Conference getConference(String name) {
       try {
      
         return DAOFactory.getInstance().getConferenceDAO().getConferenceByName(name);
       }
         catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
     
    }

    

    @Override
    public boolean deleteTimeLine(String conferenceName) {
         try {
          Timeline delTL = this.getTimeLines(conferenceName);
          DAOFactory.getInstance().getTimeLineDAO().deleteTimeline(delTL);
                
          return true;
       }
         catch(Exception e){
           System.err.println(e.getMessage());
           return false;
        }
    }
    

    @Override
    public List<Timeline> getAllTimeLines() {
      try {
      
           return DAOFactory.getInstance().getTimeLineDAO().findAllTimeline();
          
       }
        catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public Timeline getTimeLines(String conferenceName) {
       try {
      
           return DAOFactory.getInstance().getTimeLineDAO().findByCustomizeQuery("conferenceName='"+conferenceName+"'").get(0);
          
       }
        catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }

    @Override
    public List<Conference> getConferencesByType(int type) {
         try {
      
           return DAOFactory.getInstance().getConferenceDAO().
                   findByCustomizeQuery("conferencetype="+type);
          
       }
        catch(Exception e){
           System.err.println(e.getMessage());
           return null;
        }
    }


}
